package com.zpf.dao.impl;

import com.zpf.dao.LoginDao;
import com.zpf.pojo.User;
import com.zpf.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class LoginDaoImpl implements LoginDao {
    private QueryRunner queryRunner = new QueryRunner(DBUtils.getDateSource());
    @Override
    public User login(String username) {
        String sql = "select * from user where username=?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer createuser(User user) {
        String sql = "insert into `user` (username,password)values(?,?); ";
        try {
            int count = queryRunner.update(sql,user.getUsername(),user.getPassword());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
